package com.ibl.rover.lite.handler;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.ibl.rover.lite.service.utill.Core.PRODUCT;
import com.ibl.rover.lite.service.utill.HibernateUtill;
import com.ibl.rover.lite.service.utill.Core.SST_TAGS;
import com.rover.lite.hibernate.dal.RvrAppsetup;
import com.rover.lite.hibernate.dal.RvrProducts;
import com.rover.lite.hibernate.dal.RvrProductsId;
import com.ibl.rover.lite.service.utill.Core;
import org.hibernate.*;
import org.hibernate.cfg.*;

import java.util.*;
import com.ibl.rover.lite.service.sync.request.implementation.SyncRequestHandler;

public class ProductHandler {
	Session session;
	Document doc;

	public ProductHandler(Document _doc) {
		// TODO Auto-generated constructor stub

		doc = _doc;
		session = HibernateUtill.getSessionFactory().openSession();
	}

	public void getProducts(Element bodyElement, String userName) {

		// Criteria rvrProductCriteria =
		// session.createCriteria(RvrProducts.class);

		String sql_query = "SELECT DISTINCT prdC.id.categcode, prdC.id.tsoCode FROM RvrPrdcategory prdC , RvrProducts prd  WHERE prdC.id.categcode = prd.id.categcode AND  prdC.id.tsoCode='"
				+ userName + "'";
		Query query = session.createQuery(sql_query);

		Element entityElement = doc.createElement("entity");
		entityElement.setAttribute("name", SST_TAGS.PRODUCT);
		bodyElement.appendChild(entityElement);

		Element addElement = doc.createElement("add");
		entityElement.appendChild(addElement);

		for (Iterator it = query.iterate(); it.hasNext();) {
			Object[] row = (Object[]) it.next();
			String Sql_query2 = "SELECT  prd.id.brandcode,prd.id.categcode,prd.id.fulldesc,prd.id.unitprice,prd.id.code,prd.id.displayseq FROM RvrProducts prd WHERE CATEGCODE='"
					+ row[0].toString() + "' AND  TSO_CODE='" + userName + "'";

			Query query2 = session.createQuery(Sql_query2);

			for (Iterator it2 = query2.iterate(); it2.hasNext();) {
				Object[] row2 = (Object[]) it2.next();
//				System.out.println("ID: " + row2[0]);

				Element itemElement = doc.createElement("item");
				addElement.appendChild(itemElement);
				RvrProducts rvrProducts = new RvrProducts();
				RvrProductsId id = new RvrProductsId();
				id.setCode(String.valueOf(row2[4]));
				rvrProducts.setId(id);
				rvrProducts.getId().setBrandcode(String.valueOf(row2[0]));
				rvrProducts.getId().setCategcode(String.valueOf(row2[1]));
				rvrProducts.getId().setFulldesc(String.valueOf(row2[2]));
				rvrProducts.getId().setUnitprice(
						Double.parseDouble(String.valueOf(row2[3])));
				rvrProducts.getId().setDisplayseq(
						Integer.parseInt(String.valueOf(row2[5])));

				setRvrProductsAttribute(rvrProducts, itemElement);

			}

		}

	}

	private void setRvrProductsAttribute(RvrProducts rvrProducts,
			Element itemElement) {

		try {
			ArrayList<String> columnName = new ArrayList<String>();
			ArrayList<String> columnValue = new ArrayList<String>();

			columnName.add(PRODUCT.BRANDCODE);
			columnValue.add(String.valueOf(rvrProducts.getId().getBrandcode()));

			columnName.add(PRODUCT.CATEGCODE);
			columnValue.add(String.valueOf(rvrProducts.getId().getCategcode()));

			columnName.add(PRODUCT.FULLDESC);
			columnValue.add(String.valueOf(rvrProducts.getId().getFulldesc()));

			columnName.add(PRODUCT.CODE);
			columnValue.add(String.valueOf(rvrProducts.getId().getCode()));

			columnName.add(PRODUCT.UNITPRICE);
			columnValue.add(String.valueOf(rvrProducts.getId().getUnitprice()));

			columnName.add(PRODUCT.DISP_SEQ);
			columnValue
					.add(String.valueOf(rvrProducts.getId().getDisplayseq()));

			Core.setAttribute(columnName, columnValue, itemElement, doc);
		} catch (Exception e) {

		}

	}

}
